import { defineConfig } from 'vite'
import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import copy from 'rollup-plugin-copy'
import svgLoader from 'vite-svg-loader'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  server: {
    port: 5020,
    proxy: {
      '/api': {
        // target: 'http://192.168.1.57:8081',
        target: 'https://web.yunbuye.cn/test/cms-api',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
      '/oa': {
        target: 'https://web.yunbuye.cn/oa-api-test',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/oa/, ''),
      },
      //   '/api': {
      //     target: 'https://vuefinder.ozdemir.be/vuefinder',
      //     changeOrigin: true,
      //     rewrite: (path) => path.replace(/^\/api/, ''),
      //   },
    },
  },
  plugins: [
    vue(),
    svgLoader(),
    copy({
      targets: [
        { src: 'src/locales/*', dest: 'dist/locales' },
        { src: 'src/features.js', dest: 'dist' },
      ],
      hook: 'writeBundle',
    }),
    Components({
      resolvers: [
        AntDesignVueResolver({
          importStyle: false, // css in js
        }),
      ],
    }),
  ],
  css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler', // or 'modern'
      },
    },
  },
  build: {
    assetsInclude: ['/**/*.png', '//**/*.jpg', '//**/*.svg'],
    lib: {
      entry: resolve(__dirname, 'src/index.js'),
      formats: ['es', 'cjs'],
      name: 'VueFinder',
      // the proper extensions will be added
      fileName: 'vuefinder',
    },
    rollupOptions: {
      // make sure to externalize deps that shouldn't be bundled
      // into your library
      external: [
        'vue',
        'mitt',
        'vanilla-lazyload',
        'dragselect',
        'cropperjs/dist/cropper.css',
        'cropperjs',
        '@uppy/core',
        '@uppy/xhr-upload',
      ],
      output: {
        // Provide global variables to use in the UMD build
        // for externalized deps
        globals: {
          vue: 'Vue',
        },
      },
      // 关键配置：复制静态资源
      assetsDir: 'components/icons',
      copyPublicDir: false, // 禁用默认 public 复制
    },
  },
})
